<?php

/**
 * Url helper with an interface similar to the url action helper and then some.
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version   SVN: $Id: SimpleUrl.php 27 2009-05-09 18:26:19Z monzee $
 * @todo Make this a part of the Mz library
 */
class Crud_View_Helper_SimpleUrl extends Zend_View_Helper_Abstract
{
    protected $_params = array();

    /**
     * Constructor. Null param = pull from request object.
     *
     * @param string $action
     * @param string $controller
     * @param string $module
     */
    public function __construct($action = null, $controller = null, $module = null)
    {
        $this->simpleUrl($action, $controller, $module);
    }

    /**
     * If any of the parameters == null, the corresponding value will be pulled
     * from the request object.
     *
     * A new instance is returned every time this is called.
     *
     * @param string $action
     * @param string $controller
     * @param string $module
     * @return Crud_SimpleUrl
     */
    public function simpleUrl($action = null, $controller = null, $module = null)
    {
        if (!empty($this->_params)) {
            $helper = new self($action, $controller, $module);
            $helper->setView($this->view);
            return $helper;
        }

        $req = Zend_Controller_Front::getInstance()->getRequest();

        $this->_params['action'] = null === $action ? $req->getActionName() : $action;
        $this->_params['controller'] = null === $controller ? $req->getControllerName() : $action;
        $this->_params['module'] = null === $module ? $req->getModuleName() : $module;

        return $this;
    }

    /**
     * Sets a url parameter.
     *
     * @param string $param
     * @param mixed  $val
     * @return Crud_SimpleUrl
     */
    public function setParam($param, $val)
    {
        $this->_params[$param] = $val;
        return $this;
    }

    /**
     * Sugar on top of setParam().
     *
     * Usage: $this->simpleUrl()->id(123)->foo('bar')->baz('a', 'b', 'c')
     *             ->bat(array('one' => 1, 'two' => 2))->quux();
     *
     * @param string $method
     * @param array  $args
     * @return Crud_SimpleUrl
     */
    public function __call($method, $args)
    {
        $argc = count($args);
        switch ($argc) {
            case 0:
                $val = null;
                break;
            case 1:
                $val = $args[0];
                break;
            default:
                $val = $args;
                break;
        }

        return $this->setParam($method, $val);
    }

    /**
     * Returns the parameter array.
     *
     * @return array
     */
    public function toArray()
    {
        return $this->_params;
    }

    /**
     * Builds the url.
     *
     * @return Zend_View_Helper_Url
     */
    public function __toString()
    {
        return $this->view->url($this->_params, null, true);
    }

}
